import ReadImage from "@/utils/FileUtils/ReadImage";
export type ElementMap = { [name in FileName]: HTMLImageElement };

export type FileSize = [number, number, number, number];
const size32: FileSize = [0, 0, 32, 32];
export const imageFile = [
	['Battle1', 'Battle1.png', size32],
	['Door1', 'Door1.png', size32],
	['Door2', 'Door2.png', size32],
	['Door3', 'Door3.png', size32],
	['Door4', 'Door4.png', size32],
	['Enemy1', 'Enemy1.png', size32],
	['Enemy2', 'Enemy2.png', size32],
	['Enemy3', 'Enemy3.png', size32],
	['Enemy4', 'Enemy4.png', size32],
	['Enemy5', 'Enemy5.png', size32],
	['Enemy6', 'Enemy6.png', size32],
	['Enemy7', 'Enemy7.png', size32],
	['Enemy8', 'Enemy8.png', size32],
	['Enemy9', 'Enemy9.png', size32],
	['Enemy10', 'Enemy10.png', size32],
	['Enemy11', 'Enemy11.png', size32],
	['Enemy12', 'Enemy12.png', size32],
	['Enemy13', 'Enemy13.png', size32],
	['Enemy14', 'Enemy14.png', size32],
	['Enemy15', 'Enemy15.png', size32],
	['Floor', 'Floor.png', size32],
	['Gem', 'Gem.png', size32],
	['Hero', 'Hero.png', size32],
	['Item1', 'Item1.png', size32],
	['Item2', 'Item2.png', size32],
	['Item3', 'Item3.png', size32],
	['Lava', 'Lava.png', size32],
	['Npc1', 'Npc1.png', size32],
	['Npc2', 'Npc2.png', size32],
	['Npc3', 'Npc3.png', size32],
	['Stair', 'Stair.png', size32],
	['Star', 'Star.png', size32],
	['Store', 'Store.png', size32],
	['Terrain', 'Terrain.png', size32],
	['Water', 'Water.png', size32],
	['Weapon', 'Weapon.png', size32],
	['GoFloor', 'GoFloor.png', size32],
] as const;


export type FileName = typeof imageFile[number][0];
export type FileMap<T = typeof imageFile[number]> = { [name in FileName]: T };
export type FileInfo = typeof imageFile[number];

const imageMap = {} as FileMap;
for (const info of imageFile) {
	const name = info[0];
	imageMap[name] = info;
}

export type FilePos = readonly [number, number];
const R4 = (row: number): readonly FilePos[] => [[0, row], [1, row], [2, row], [3, row]];
const C4 = (rol: number): readonly FilePos[] => [[rol, 0], [rol, 1], [rol, 2], [rol, 3]];
const Pos = (col: number, row: number): FilePos => [col, row];

export type ImageInfoT<T> = readonly [id: T, image: FileName, pos: readonly FilePos[]];
const MAIL = <T>(data: readonly ImageInfoT<T>[]) => data;

export const imageSplit = MAIL(<const>[
	["闪星星", "Star", R4(0)], ["Lava", "Lava", R4(0)], ["Water", "Water", R4(0)],
	["Terrain1", "Terrain", [[0, 0]]], ["Terrain2", "Terrain", [[1, 0]]], ["石墙", "Terrain", [[2, 0]]],
	["Terrain4", "Terrain", [[0, 1]]], ["Terrain5", "Terrain", [[1, 1]]], ["石地板", "Terrain", [[2, 1]]],

	["黄门", "Door1", C4(0)], ["蓝门", "Door1", C4(1)], ["红门", "Door1", C4(2)], ["青门", "Door1", C4(3)],
	["Door5", "Door2", C4(0)], ["Door6", "Door2", C4(1)], ["石头门", "Door2", C4(2)], ["铁门", "Door2", C4(3)],
	["Door9", "Door3", C4(0)], ["Door10", "Door3", C4(1)], ["Door11", "Door3", C4(2)], ["Door12", "Door3", C4(3)],
	["Door13", "Door4", C4(0)], ["Door14", "Door4", C4(1)], ["隐藏石墙", "Door4", C4(2)], ["Door16", "Door4", C4(3)],
	["楼梯下", "Stair", [[0, 0]]], ["楼梯上", "Stair", [[1, 0]]], ["Stair3", "Stair", [[2, 0]]],

	["老人", "Npc1", R4(0)], ["奸商", "Npc1", R4(1)], ["小偷", "Npc1", R4(2)], ["仙子", "Npc1", R4(3)],
	["Npc5", "Npc2", R4(0)], ["Npc6", "Npc2", R4(1)], ["贪婪之神", "Npc2", R4(2)], ["公主", "Npc2", R4(3)],
	["魔命棋妙", "Npc3", R4(0)], ["Npc10", "Npc3", R4(1)], ["Npc11", "Npc3", R4(2)], ["Npc12", "Npc3", R4(3)],

	["英雄下", "Hero", R4(0)], ["英雄左", "Hero", R4(1)], ["英雄右", "Hero", R4(2)], ["英雄上", "Hero", R4(3)], ["勇士", "Hero", [[0, 0]]],

	["商店1_1", "Store", [[0, 0]]], ["商店1_2", "Store", [[1, 0], [1, 0], [3, 0], [3, 0]]], ["商店1_3", "Store", [[2, 0]]],
	["商店2_1", "Store", [[0, 1]]], ["商店2_2", "Store", [[1, 1], [1, 1], [3, 1], [3, 1]]], ["商店2_3", "Store", [[2, 1]]],
	["商店3_1", "Store", [[0, 2]]], ["商店3_2", "Store", [[1, 2], [1, 2], [3, 2], [3, 2]]], ["商店3_3", "Store", [[2, 2]]],

	["红宝石", "Gem", [[0, 0]]], ["蓝宝石", "Gem", [[1, 0]]], ["绿宝石", "Gem", [[2, 0]]], ["黄宝石", "Gem", [[3, 0]]],
	["小血瓶", "Gem", [[0, 1]]], ["大血瓶", "Gem", [[1, 1]]], ["敏捷药水", "Gem", [[2, 1]]], ["经验药水", "Gem", [[3, 1]]],
	["Gem9", "Gem", [[0, 2]]], ["Gem10", "Gem", [[1, 2]]], ["Gem11", "Gem", [[2, 2]]], ["Gem12", "Gem", [[3, 2]]],
	["Gem13", "Gem", [[2, 3]]], ["圣水", "Gem", [[3, 3]]],
	["黄钥匙", "Item1", [[0, 0]]], ["蓝钥匙", "Item1", [[1, 0]]], ["红钥匙", "Item1", [[2, 0]]], ["绿钥匙", "Item1", [[3, 0]]],
	["铁门钥匙", "Item1", [[0, 1]]], ["普通钥匙盒", "Item1", [[1, 1]]], ["宝石钥匙盒", "Item1", [[2, 1]]], ["经验卷轴", "Item1", [[3, 1]]],
	["地震卷轴", "Item1", [[0, 2]]], ["怪物图鉴", "Item1", [[1, 2]]], ["奇怪的权杖", "Item1", [[2, 2]]], ["幸运金币", "Item1", [[3, 2]]],
	["Item13", "Item1", [[0, 3]]], ["飞羽", "Item1", [[1, 3]]], ["下楼器", "Item1", [[2, 3]]], ["上楼器", "Item1", [[3, 3]]],
	["Item17", "Item2", [[0, 0]]], ["Item18", "Item2", [[1, 0]]], ["Item19", "Item2", [[2, 0]]], ["Item20", "Item2", [[3, 0]]],
	["罪恶权杖", "Item2", [[0, 1]]], ["Item22", "Item2", [[1, 1]]], ["Item23", "Item2", [[2, 1]]], ["Item24", "Item2", [[3, 1]]],
	["Item25", "Item2", [[0, 2]]], ["Item26", "Item2", [[1, 2]]], ["圣十字架", "Item2", [[2, 2]]], ["冰冻徽章", "Item2", [[3, 2]]],
	["屠龙匕", "Item2", [[0, 3]]], ["炸弹", "Item2", [[1, 3]]], ["Item31", "Item2", [[2, 3]]], ["破墙镐", "Item2", [[3, 3]]],
	["Item33", "Item3", [[0, 0]]], ["Item34", "Item3", [[1, 0]]], ["随意门", "Item3", [[2, 0]]], ["血量", "Item3", [[3, 0]]],
	["铁剑", "Weapon", [[0, 0]]], ["银剑", "Weapon", [[1, 0]]], ["骑士剑", "Weapon", [[2, 0]]],
	["圣剑", "Weapon", [[3, 0]]], ["神圣剑", "Weapon", [[0, 1]]],
	["铁盾", "Weapon", [[0, 2]]], ["银盾", "Weapon", [[1, 2]]], ["骑士盾", "Weapon", [[2, 2]]],
	["圣盾", "Weapon", [[3, 2]]], ["神圣盾", "Weapon", [[0, 3]]],

	["魔龙左翼", "Enemy1", R4(3)], ["魔龙心", "Enemy1", R4(2)], ["魔龙右翼", "Enemy1", R4(4)],
	["魔龙左尖刺", "Enemy1", R4(5)], ["魔龙角", "Enemy1", R4(1)], ["魔龙右尖刺", "Enemy1", R4(6)],
	["魔龙左爪", "Enemy1", R4(7)], ["魔龙头", "Enemy1", R4(0)], ["魔龙右爪", "Enemy1", R4(8)],
	["Battle1", "Battle1", R4(0)], ["Battle2", "Battle1", R4(1)], ["Battle3", "Battle1", R4(2)], ["Battle4", "Battle1", R4(3)],
	["小蝙蝠", "Enemy2", R4(0)], ["大蝙蝠", "Enemy2", R4(1)], ["红蝙蝠", "Enemy2", R4(2)], ["冥灵分身", "Enemy2", R4(3)],
	["骷髅人", "Enemy3", R4(0)], ["骷髅武士", "Enemy3", R4(1)], ["骷髅队长", "Enemy3", R4(2)], ["冥队长", "Enemy3", R4(3)],
	["兽人", "Enemy4", R4(0)], ["兽人武士", "Enemy4", R4(1)], ["石头人", "Enemy4", R4(2)], ["幽灵", "Enemy4", R4(3)],
	["初级法师", "Enemy5", R4(0)], ["高级法师", "Enemy5", R4(1)], ["麻衣法师", "Enemy5", R4(2)], ["无界法师", "Enemy5", R4(3)],
	["初级卫兵", "Enemy6", R4(0)], ["中级卫兵", "Enemy6", R4(1)], ["高级卫兵", "Enemy6", R4(2)], ["双手剑士", "Enemy6", R4(3)],
	["冥斗士", "Enemy7", R4(0)], ["骑士队长", "Enemy7", R4(1)], ["圣骑士", "Enemy7", R4(2)], ["黑暗骑士", "Enemy7", R4(3)],
	["ZENO分身", "Enemy8", R4(0)], ["弱化ZENO", "Enemy8", R4(1)], ["魔法卫兵", "Enemy8", R4(2)], ["骑士护卫", "Enemy8", R4(3)],
	["绿史莱姆", "Enemy9", R4(0)], ["红史莱姆", "Enemy9", R4(1)], ["黑史莱姆", "Enemy9", R4(2)], ["史莱姆魔皇", "Enemy9", R4(3)],
	["金史莱姆", "Enemy10", R4(0)], ["冥骷髅", "Enemy10", R4(1)], ["冥灵魔王", "Enemy10", R4(2)], ["Enemy36", "Enemy10", R4(3)],
	["Enemy37", "Enemy11", R4(0)], ["魔物首领古顿", "Enemy11", R4(1)], ["Enemy39", "Enemy11", R4(2)], ["Enemy40", "Enemy11", R4(3)],
	["Enemy41", "Enemy12", R4(0)], ["破坏神分身", "Enemy12", R4(1)], ["远古护卫", "Enemy12", R4(2)], ["史莱姆王", "Enemy12", R4(3)],
	["魔王ZENO", "Enemy13", R4(0)], ["魔法警卫", "Enemy13", R4(1)], ["大法师分身", "Enemy13", R4(2)], ["银史莱姆", "Enemy13", R4(3)],
	["Enemy49", "Enemy14", R4(0)], ["梦魇", "Enemy14", R4(1)], ["模仿者", "Enemy14", R4(2)], ["魔化仙子", "Enemy14", R4(3)],
	["Enemy53", "Enemy15", R4(0)], ["中级法师", "Enemy15", R4(1)], ["Enemy55", "Enemy15", R4(2)], ["Enemy56", "Enemy15", R4(3)],
	["Floor", "Floor", [[0, 0]]],
	["GoFloor", "GoFloor", [[0, 0]]],
]);

export type ImageName = typeof imageSplit[number][0];
export type ImageInfo = typeof imageSplit[number];

export type ImageMap<T = typeof imageSplit[number]> = { [name in ImageName]: ImageInfoT<T> };

const imageSplitMap: ImageMap = {} as any;

for (const image of imageSplit) {
	(<any>imageSplitMap)[image[0]] = image;
}

const imageElementMap: ElementMap = {} as any;

export async function ReadImageAll(): Promise<void> {
	for (const [name, fileName] of imageFile) {
		imageElementMap[name] = await ReadImage(`./images/${fileName}`);
	}
};

export function GetImageElement(name: FileName) {
	return imageElementMap[name];
};

export function GetImage(name: ImageName): readonly [FileInfo, readonly FilePos[]] {
	const [, fileName, position] = imageSplitMap[name];
	return [imageMap[fileName], position];
};
